package com.fr.base.background;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.serializable.ImageSerializable;
import com.fr.chart.core.ChartUtils;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ImageBackground extends AbstractBackground
{
  private transient Image image;
  private int layout;
  private double specifiedImageWidth;
  private double specifiedImageHeight;

  public ImageBackground()
  {
    this(null);
  }

  public ImageBackground(Image paramImage)
  {
    this(paramImage, 0);
  }

  public ImageBackground(Image paramImage, int paramInt)
  {
    this.image = null;
    this.layout = 3;
    this.specifiedImageWidth = -1.0D;
    this.specifiedImageHeight = -1.0D;
    setImage(paramImage);
    setLayout(paramInt);
  }

  public Image getImage()
  {
    return this.image;
  }

  public void setImage(Image paramImage)
  {
    this.image = paramImage;
  }

  public int getLayout()
  {
    return this.layout;
  }

  public void setLayout(int paramInt)
  {
    this.layout = paramInt;
  }

  public Image getDefaultImage()
  {
    int k;
    int l;
    int i = (int)this.specifiedImageWidth;
    int j = (int)this.specifiedImageHeight;
    if ((i == -1) && (j == -1))
      return getImage();
    if (i == -1)
      k = getImage().getWidth(null);
    else
      k = i;
    if (j == -1)
      l = getImage().getHeight(null);
    else
      l = j;
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage(k, l, 2);
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    paint(localGraphics2D, new Rectangle2D.Double(0.0D, 0.0D, k, l));
    localBufferedImage.flush();
    localGraphics2D.dispose();
    return localBufferedImage;
  }

  public double getSpecifiedImageWidth()
  {
    return this.specifiedImageWidth;
  }

  public void setSpecifiedImageWidth(double paramDouble)
  {
    this.specifiedImageWidth = paramDouble;
  }

  public double getSpecifiedImageHeight()
  {
    return this.specifiedImageHeight;
  }

  public void setSpecifiedImageHeight(double paramDouble)
  {
    this.specifiedImageHeight = paramDouble;
  }

  public void paint(Graphics paramGraphics, Shape paramShape)
  {
    if (getImage() == null)
      return;
    Rectangle2D localRectangle2D = paramShape.getBounds2D();
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Shape localShape = localGraphics2D.getClip();
    localGraphics2D.clip(paramShape);
    localGraphics2D.translate(localRectangle2D.getX(), localRectangle2D.getY());
    GraphHelper.paintImage(localGraphics2D, (int)localRectangle2D.getWidth(), (int)localRectangle2D.getHeight(), getImage(), null, getLayout(), (int)getSpecifiedImageWidth(), (int)getSpecifiedImageHeight());
    localGraphics2D.translate(-localRectangle2D.getX(), -localRectangle2D.getY());
    localGraphics2D.setClip(localShape);
  }

  public void paint2(Graphics paramGraphics, Shape paramShape, int paramInt1, int paramInt2)
  {
    if (getImage() == null)
      return;
    Rectangle2D localRectangle2D = paramShape.getBounds2D();
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Shape localShape = localGraphics2D.getClip();
    localGraphics2D.clip(paramShape);
    localGraphics2D.translate(localRectangle2D.getX(), localRectangle2D.getY());
    GraphHelper.paintImage2(localGraphics2D, (int)localRectangle2D.getWidth(), (int)localRectangle2D.getHeight(), getImage(), null, getLayout(), (int)getSpecifiedImageWidth(), (int)getSpecifiedImageHeight(), paramInt1, paramInt2);
    localGraphics2D.translate(-localRectangle2D.getX(), -localRectangle2D.getY());
    localGraphics2D.setClip(localShape);
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws ClassNotFoundException, IOException
  {
    paramObjectInputStream.defaultReadObject();
    Object localObject = paramObjectInputStream.readObject();
    if (localObject != null)
      this.image = ((ImageSerializable)localObject).getImage();
  }

  private void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    paramObjectOutputStream.defaultWriteObject();
    if (this.image != null)
    {
      ImageSerializable localImageSerializable = new ImageSerializable(this.image);
      paramObjectOutputStream.writeObject(localImageSerializable);
    }
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof ImageBackground))
      return false;
    ImageBackground localImageBackground = (ImageBackground)paramObject;
    return ((getLayout() == localImageBackground.getLayout()) && (ComparatorUtils.imageEquals(getImage(), localImageBackground.getImage())) && (getSpecifiedImageWidth() == localImageBackground.getSpecifiedImageWidth()) && (getSpecifiedImageHeight() == localImageBackground.getSpecifiedImageHeight()));
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = new JSONObject();
    localJSONObject.put("layout", this.layout);
    localJSONObject.put("specifiedImageHeight", this.specifiedImageHeight);
    localJSONObject.put("specifiedImageWidth", this.specifiedImageWidth);
    if (getImage() != null)
    {
      localJSONObject.put("imgWidth", getImage().getWidth(null));
      localJSONObject.put("imgHeight", getImage().getHeight(null));
      ChartUtils.image2JS(localJSONObject, getImage());
    }
    return localJSONObject;
  }

  public String getBackgroundType()
  {
    return "ImageBackground";
  }
}